﻿using System.IO;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xom;
using Xom.Engine;
using XomSample;

namespace XOMSample.UnitTests
{
    [TestClass]
    public class LibraryTest
    {
        private static XmlDocumentEngine _xom;

        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            _xom = new XmlDocumentEngine();
        }

        /// <summary>
        /// A test for reading the library sample xml file
        /// </summary>
        [TestMethod]
        public void ReadLibraryTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleLibraryXmlFile();
            //act
            ILibrary library = _xom.StartMapping(sampleXml.LastChild, typeof(Library)) as ILibrary;

            //assert
            Assert.IsNotNull(library);
            Assert.AreEqual(1, library.Books.Count);
            Assert.IsNotNull(library.Books[0].Title);
            Assert.AreEqual(1, library.Books[0].Authors.Count);
            Assert.AreEqual(4, library.Books[0].Characters.Count);
            Assert.AreEqual("b0836217462", library.Books[0].Id);
            Assert.AreEqual(836217462, library.Books[0].Isbn);
            Assert.AreEqual(true, library.Books[0].IsAvailable);
            Assert.AreEqual("en", library.Books[0].Title.Language);
            Assert.AreEqual("Being a Dog Is a Full-Time Job", library.Books[0].Title.Name);
            Assert.AreEqual("CMS", library.Books[0].Authors[0].Id);
            Assert.AreEqual("Charles M Schulz", library.Books[0].Authors[0].Name);
            Assert.AreEqual("Snoopy", library.Books[0].Characters[1].Id);
        }

        /// <summary>
        /// A test for reading the library sample xml file with linq to xml
        /// </summary>
        [TestMethod()]
        public void ReadLibraryWithLinqTest()
        {
            //arrange
            //act
            ILibrary library = XmlObjectMapper.Parse(GetSampleLibraryXmlFile().OuterXml, typeof(Library)) as ILibrary;
            //assert
            Assert.IsNotNull(library);
            Assert.AreEqual(1, library.Books.Count);
            Assert.IsNotNull(library.Books[0].Title);
            Assert.AreEqual(1, library.Books[0].Authors.Count);
            Assert.AreEqual(4, library.Books[0].Characters.Count);
            Assert.AreEqual("b0836217462", library.Books[0].Id);
            Assert.AreEqual(836217462, library.Books[0].Isbn);
            Assert.AreEqual(true, library.Books[0].IsAvailable);
            Assert.AreEqual("en", library.Books[0].Title.Language);
            Assert.AreEqual("Being a Dog Is a Full-Time Job", library.Books[0].Title.Name);
            Assert.AreEqual("CMS", library.Books[0].Authors[0].Id);
            Assert.AreEqual("Charles M Schulz", library.Books[0].Authors[0].Name);
            Assert.AreEqual("Snoopy", library.Books[0].Characters[1].Id);
        }

        private XmlDocument GetSampleLibraryXmlFile()
        {
            XmlDocument xmlFile = new XmlDocument();
            xmlFile.Load(new MemoryStream(Properties.Resources.library));

            return xmlFile;
        }

        /// <summary>
        /// Change the title object
        /// </summary>
        [TestMethod()]
        public void ChangeTitleTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleLibraryXmlFile();
            ILibrary library = _xom.StartMapping(sampleXml.LastChild, typeof(Library)) as ILibrary;

            //act
            library.Books[0].Title = new Title{ Language = "de", Name = "Snoopy Buch"};

            //assert
            Assert.IsNotNull(library.Books[0].Title);
            Assert.AreEqual("de", sampleXml.ChildNodes[2].FirstChild.ChildNodes[1].Attributes[0].Value);
            Assert.AreEqual("Snoopy Buch", sampleXml.ChildNodes[2].FirstChild.ChildNodes[1].InnerText);
        }

        [TestMethod()]
        public void AddNewBookToLibraryTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleLibraryXmlFile();
            ILibrary library = _xom.StartMapping(sampleXml.LastChild, typeof(Library)) as ILibrary;

            //act
            IBook2 newLibraryBook = new Book2();
            newLibraryBook.Id = "b00000001";
            newLibraryBook.Isbn = 1;
            newLibraryBook.Title = new Title { Language = "en", Name = "New Book" };
            library.Books.Add(newLibraryBook);

            //assert
            Assert.AreEqual(2, library.Books.Count);
            Assert.AreEqual("b00000001", sampleXml.LastChild.ChildNodes[1].Attributes[0].Value);
            Assert.AreEqual("1", sampleXml.LastChild.ChildNodes[1].ChildNodes[0].InnerText);
            Assert.AreEqual("en", sampleXml.LastChild.ChildNodes[1].ChildNodes[1].Attributes[0].Value);
            Assert.AreEqual("New Book", sampleXml.LastChild.ChildNodes[1].ChildNodes[1].InnerText);
        }
    }
}
